/* ============================================================================
* 功能描述 ：主函数
* 作者       ：ggls
* 创建日期 ：2024.04.10
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/


#include "main.h"
bit time_f;
char alarm_temp;
bit buzz_on;
bit alarm_io_f;
unsigned long now_time_f;
char recording_time;
unsigned long last_time_f;
char io_delay;
bit remove_alarm;
bit buzz_kg =0;
bit delay_io_alarm;
bit led_switch;
unsigned long led_switch_time;
char sj = 1;

bit key_flag;

bit RXOK_f;


void _data_tx_pack(void)
{
	char ap_data[10];
	sj ++;
	ap_data[0] = 0x01;
	ap_data[1] = 0x02;
	ap_data[2] = 0x03;
	ap_data[3] = 0x04;
	ap_data[4] = 0x05;
	ap_data[5] = 0x06;
	ap_data[6] = sj;
	ap_data[7] = 0x06;
	ap_data[8] = 0x06;
	Uart_Tx_buf(ap_data, 9);
	
	
	

}


void main(void)
{
	char ch;
	Init_System();			// 系统初始化			
	Time1_Init();			// 时钟1初始化

	Uart_Init();			// 串口初始化
	
	ADC_INIT();				// adc初始化
	
	Zigbee_wake_up_init();		// zigbee模块唤醒
	Hly_Water_Immersion_Init();

	TRISB3 = 0;

	/* 按键io初始化 */
	TRISB4 = 1 ;
	WPUB4 = 1;
//	OSCCON = 0B01010010;			 // 看门狗配置
//	OPTION_REG = 0B00001110;		// 看门狗配置


//	app_init();
	Zigbee_Led_init();
	hly_pwm_init();

	Zigbee_App_Init();		// 初始化zigbee参数
//	now_time_f = 0;
//	key_flag = 0;
//	last_time_f = 0;
//	time_f = 0;
//	Hly_Adc_update();
	delay_io_alarm = 0;
	io_delay = 0;
	led_switch = 1;

	remove_alarm = 0;
	while(1)
	{
		//	RB5 = 0;
//	asm("clrwdt");
		ch = Hly_Obtain_Water_val();
		if(led_switch != alarm_io_f)
		{
			Zigbee_led_on();
			if(hly_get_elapsed_ms(led_switch_time) >= 33)
			{
				led_switch_time = get_current_ms();
				led_switch = 0;
				led_switch = alarm_io_f;
				Zigbee_led_off();
			}
		
		}

		if(ch == 1)
		{
			if(delay_io_alarm == 0)
			{
				alarm_io_f = 1;
				Zigbee_wake_up();
				DelayXms(15);
				Hly_zigbee_water_alarm();
				Zigbee_sleep();
			}
			if(hly_get_elapsed_ms(app_data.alarm_io_time) >= 10)
			{
				app_data.alarm_io_time = get_current_ms();
				io_delay++;
			}
			
			if(io_delay == 200)
			{
				io_delay = 0;
				delay_io_alarm = 0;

			}
			
		}
		else if(ch == 0)
		{
			PWM1EN = 0;
			delay_io_alarm = 0;
			remove_alarm = 0;
			if(alarm_io_f == 1)
			{
				Zigbee_wake_up();
				DelayXms(15);
				Hly_zigbee_water_normal();
				alarm_io_f = 0;
				Zigbee_sleep();
			}
			
		}
		
		
		if(alarm_io_f == 1 && remove_alarm == 0)
		{
			delay_io_alarm = 1;
			if(hly_get_elapsed_ms(app_data.buzz_time) >= 5)
			{
				app_data.buzz_time = get_current_ms();
				if(buzz_kg == 0)
				{
					PWM1EN = 1;
					buzz_kg = 1;
				}
				else if(buzz_kg == 1)
				{
					PWM1EN = 0;
					buzz_kg = 0;
					buzz_on = 1;
					alarm_temp ++;
				}
	
			}
			if(alarm_temp == 66)
			{
				alarm_io_f = 0;
				buzz_on = 0;
				alarm_temp = 0;
			}
			
		}
		else if(remove_alarm == 1)
		{
			PWM1EN = 0;
		}
		//key_on();
		
		App_Run();   // 应用运行！
	//	Zigbee_sleep();
	//	DelayXms(200);
	//	Sleep_Mode();
	//_nop();
	
	//	asm("sleep");				//进入休眠模式
	
		asm("nop");
		
			
	

#if 0


	//Hly_Temp_Hum_Obtain();
			
			DelayXms(200);
		//	Zigbee_sleep();
		//	asm("sleep");				//进入休眠模式
			//key_on();

			RB5 = 0;
		RB4 = 0;
		
		RB5 = ~RB5;
		RB4 = ~RB4;
		_nop();
		_nop();
		RB5 = ~RB5;
		RB4 = ~RB4;
		_nop();
		
		_nop();
		RB5 = ~RB5;
		RB4 = ~RB4;
		_nop();
		_nop();
		
		RB5 = ~RB5;
		RB4 = ~RB4;

		i = TMR1L;
		if (i == 0xff)
		{
		
			_data_tx_pack();
			TMR1L = 0;
			TMR1H = 0;
			time_f++;
		}
			
#endif	

		
		
		
		

		
#if 0
		if(RXOK_f==1)
		{
			for(i=0;i<8;i++)
			{
					
				TXREG =RxTable[i];

				while(TRMT==0);
			}

			RXOK_f = 0;
		}
#endif
	}
}
void key_on(void)
{
//	HLY_GetTick();
	if(RB4 == 0)
	{
		DelayXms(10);  // 防抖


		if(RB4 == 0)
		{
			if(key_flag == 0)
			{
				last_time_f = now_time_f;
				time_f = 1;
			}
			if(abs(now_time_f - last_time_f) == 166 && time_f == 1)
			{
				Zigbee_wake_up();
				DelayXms(15);
				Zigbee_Network_Status();
				
				Zigbee_sleep();
				time_f = 0;
			}
			key_flag = 1;
		}
		
	}
	else
	{
		DelayXms(10);        
		if(RB4 == 1 && key_flag == 1 )   //检测弹起
		{
			//_data_tx_pack();             //弹起后处理任务
			if(time_f == 1)
			{
				remove_alarm = 1;
				time_f = 0;

			}
			time_f = 0;
			key_flag = 0;
		
			
		}			
	}
}

unsigned long get_current_ms(void)
{
    return now_time_f;
}


/***********************************************
函数名称：Init_System
函数功能：系统初始化
入口参数：无
出口参数：无
备注：
************************************************/
void Init_System()
{
	asm("nop");
	asm("clrwdt");
	INTCON = 0;					//系统初始化
	OSCCON = 0X70;				//配置振荡为16M,关WDT
	OPTION_REG = 0;

	PEIE = 1;
	RCIE = 1;
	GIE  = 1;

}


/***********************************************
函数名称：Time1_Init
函数功能：time1初始化
入口参数：无
出口参数：无
备注：
************************************************/

void Time1_Init()
{
	
	
	
	T1GINV = 0;
	
	TMR1GE = 0;	
	
	T1CKPS1 = 1;
	T1CKPS0 = 1;
	
	
	T1SYNC = 1;

	
	
	T1OSCEN = 1;
	TMR1ON = 1;
	
	TMR1H = 0;
	TMR1L = 0;

	TMR1IE = 1;


	
}

void Sleep_Mode()
{
	
	EECON1 = 0;					//清此寄存器,有好处
	TRISB1 = 0;
	TRISB0 = 0;
	
	RB0 = 0;
	RB1 = 0;

	ADCON0 = 0;					//关闭所有模块
	
	IOCA = 0B00100000;			//允许RA5的IO口电平变化中断
	RAIE = 1;					//允许PORTB电平变化中断
	GIE = 1;					//GIE = 0时，唤醒后执行SLEEP后程序;GIE = 1时，唤醒后跳至中断服务
	TRISB7 = 0;
	
	
	PORTB;						//读PORTB值并锁存			
	PORTA;						//读PORTA值并锁存		
	asm("clrwdt");
	asm("sleep");				//进入休眠模式
	
	asm("nop");
	//Init_System();
	
}



/***********************************************
函数名称：PB_Isr
函数功能：PB电平变化中断服务
入口参数：无
出口参数：无
备注：
************************************************/
void interrupt PB_Isr()
{
	if(RBIF)
	{
		RBIF = 0;			//清中断标志
		//RB6 = ~RB6;
		PORTB;
	
	}
	if(RAIF)
	{
		RAIF = 0;			//清中断标志
		PORTA;
	
	}
	if (TMR1IF)
	{
		TMR1L = 0;
		TMR1H = 0;
		TMR1IF = 0;
		now_time_f++;
		if(now_time_f == 50000)
		{
			now_time_f = 0;
		}
	}

#if 1	
	if(RCIF==1)					// 接收中断
	{
		uart_cmd_rx_cplt_callback();
	}

#else
	static unsigned char RxNum=0,TEMP;

	if(RCIF==1)					// 接收中断
	{
		RCIF = 0;
	    
		if(RXOK_f==0)	
		{
			RxTable[RxNum] = RCREG;
			RxNum++;
			if(RxNum > 7)
			{
				RxNum =0;
				RXOK_f =1;
			}
		}
		else
		TEMP =  RCREG;
		
	}
#endif
}

